<template>
  <div class="register-container">
    <!-- 背景图案 -->
    <div class="bg-pattern"></div>
    <div class="bg-overlay"></div>
    
    <!-- 注册卡片 -->
    <div class="register-card">
      <!-- Logo区域 -->
      <div class="logo-area">
        <t-image
          class="logo-image"
          src="https://tdesign.gtimg.com/demo/demo-image-1.png"
        ></t-image>
        <div class="app-title">一路相伴</div>
        <div class="app-slogan">专业陪玩服务平台</div>
      </div>
      
      <!-- 表单区域 -->
      <div class="form-area">
        <h2 class="form-title">注册账号</h2>
        
        <!-- 手机号输入 -->
        <div class="input-group">
          <t-input 
            class="custom-input" 
            placeholder="请输入手机号" 
            v-model="user.phone" 
            @change="checkphone"
            size="large"
            :maxlength="11"
          >
            <template #prefixIcon>
              <phone-icon />
            </template>
          </t-input>
          <div class="input-bar"></div>
          <div class="input-hint success" v-if="phoneAvailable">
            <t-icon name="check-circle-filled" size="small" /> 手机号可用
          </div>
        </div>
        
        <!-- 密码输入 -->
        <div class="input-group">
          <t-input 
            class="custom-input" 
            placeholder="请设置密码" 
            v-model="user.pass" 
            :type="passwordVisible ? 'text' : 'password'"
            size="large"
          >
            <template #prefixIcon>
              <app-icon />
            </template>
            <template #suffixIcon>
              <div @click="togglePasswordVisibility">
                <BrowseOffIcon v-if="!passwordVisible" />
                <BrowseIcon v-else />
              </div>
            </template>
          </t-input>
          <div class="input-bar"></div>
        </div>
        
        <!-- 确认密码输入 -->
        <div class="input-group">
          <t-input 
            class="custom-input" 
            placeholder="请确认密码" 
            v-model="pass_2" 
            :type="confirmPasswordVisible ? 'text' : 'password'"
            size="large"
          >
            <template #prefixIcon>
              <Password-icon />
            </template>
            <template #suffixIcon>
              <div @click="toggleConfirmPasswordVisibility">
                <BrowseOffIcon v-if="!confirmPasswordVisible" />
                <BrowseIcon v-else />
              </div>
            </template>
          </t-input>
          <div class="input-bar"></div>
          <!-- 密码匹配提示 -->
          <div class="password-hint" v-if="pass_2 && user.pass !== pass_2">
            两次输入的密码不一致
          </div>
        </div>
        
        <!-- 协议同意 -->
        <div class="agreement-check">
          <t-checkbox v-model="agreedToTerms">
            <span class="agreement-text">
              我已阅读并同意
              <span class="agreement-link" @click.stop="showTerms('用户协议')">《用户协议》</span>
              和
              <span class="agreement-link" @click.stop="showTerms('服务协议')">《服务协议》</span>
            </span>
          </t-checkbox>
        </div>
        
        <!-- 注册按钮 -->
        <t-button 
          class="register-btn" 
          shape="round" 
          block 
          @click="add"
          :loading="isLoading"
        >
          立即注册
        </t-button>
        
        <!-- 登录链接 -->
        <div class="login-link" @click="tz('/login')">
          已有账号？ <span class="highlight">立即登录</span>
        </div>
      </div>
      
      <!-- 注册优势 -->
      <div class="register-benefits">
        <div class="benefit-item">
          <t-icon name="secured" class="benefit-icon" />
          <div class="benefit-text">安全保障</div>
        </div>
        <div class="benefit-item">
          <t-icon name="discount" class="benefit-icon" />
          <div class="benefit-text">专享优惠</div>
        </div>
        <div class="benefit-item">
          <t-icon name="user-talk" class="benefit-icon" />
          <div class="benefit-text">专业陪玩</div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { useRouter } from "vue-router";
import { ref } from "vue";
import { get, post } from "../util/Request";
import { Toast } from 'tdesign-mobile-vue';
import { BrowseIcon } from 'tdesign-icons-vue-next';

// 变量定义
const user = ref({
  phone: '',
  pass: ''
});
const pass_2 = ref('');
const router = useRouter();
const passwordVisible = ref(false);
const confirmPasswordVisible = ref(false);
const agreedToTerms = ref(false);
const isLoading = ref(false);
const phoneAvailable = ref(false);

// 页面跳转
const tz = (url) => {
  router.push(url);
};

// 切换密码可见性
const togglePasswordVisibility = () => {
  passwordVisible.value = !passwordVisible.value;
};

// 切换确认密码可见性
const toggleConfirmPasswordVisibility = () => {
  confirmPasswordVisible.value = !confirmPasswordVisible.value;
};

// 展示协议内容
const showTerms = (type) => {
  Toast({
    message: `${type}内容正在准备中...`,
    theme: 'info',
    duration: 2000
  });
};

// 校验手机号是否可用
const checkphone = () => {
  phoneAvailable.value = false;
  
  if (user.value.phone.length === 11) {
    // 检查手机号是否可用
    get("user/checkphone?phone=" + user.value.phone).then(res => {
      if (res.data.code == 200) {
        // 手机号可用
        phoneAvailable.value = true;
        Toast({
          message: '手机号可用',
          theme: 'success',
          duration: 2000
        });
      } else {
        Toast({
          duration: 3000,
          theme: 'error',
          message: res.data.data
        });
      }
    }).catch(err => {
      Toast({
        duration: 2000,
        theme: 'error',
        message: '网络请求失败，请检查网络连接'
      });
    });
  }
};

// 注册账号
const add = () => {
  // 表单验证
  if (!user.value.phone) {
    Toast({ message: '请输入手机号', theme: 'warning' });
    return;
  }
  
  if (user.value.phone.length !== 11) {
    Toast({ message: '请输入正确的手机号', theme: 'warning' });
    return;
  }
  
  if (!user.value.pass) {
    Toast({ message: '请设置密码', theme: 'warning' });
    return;
  }
  
  if (!pass_2.value) {
    Toast({ message: '请确认密码', theme: 'warning' });
    return;
  }
  
  if (!agreedToTerms.value) {
    Toast({ message: '请阅读并同意协议', theme: 'warning' });
    return;
  }
  
  // 校验2次密码是否一致
  if (user.value.pass !== pass_2.value) {
    Toast({ 
      message: "两次输入的密码不一致",
      theme: 'error',
      duration: 3000
    });
    return;
  }
  
  // 开始注册
  isLoading.value = true;
  
  // 请求接口实现用户注册
  post("user/register", user.value).then(res => {
    isLoading.value = false;
    
    if (res.data.code == 200) {
      Toast({
        message: "注册成功！",
        theme: 'success',
        duration: 2000
      });
      
      // 成功跳转登录
      setTimeout(() => {
        router.push("/login");
      }, 1000);
    } else {
      Toast({
        message: res.data.data || "注册失败，请稍后再试",
        theme: 'error',
        duration: 3000
      });
    }
  }).catch(() => {
    isLoading.value = false;
    Toast({
      message: "网络请求失败，请检查网络连接",
      theme: 'error',
      duration: 3000
    });
  });
};
</script>

<style scoped>
.register-container {
  min-height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  position: relative;
  overflow: hidden;
  padding: 20px;
  background-color: #f5f7fa;
}

/* 背景样式 */
.bg-pattern {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-image: linear-gradient(135deg, #f5f7fa 25%, transparent 25%),
                    linear-gradient(225deg, #f5f7fa 25%, transparent 25%),
                    linear-gradient(45deg, #f5f7fa 25%, transparent 25%),
                    linear-gradient(315deg, #f5f7fa 25%, #e4e8f0 25%);
  background-position: 25px 0, 25px 0, 0 0, 0 0;
  background-size: 50px 50px;
  background-repeat: repeat;
  opacity: 0.3;
  z-index: 0;
}

.bg-overlay {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: linear-gradient(120deg, rgba(66, 103, 212, 0.5), rgba(83, 166, 138, 0.4));
  z-index: 0;
}

/* 注册卡片样式 */
.register-card {
  width: 100%;
  max-width: 400px;
  background-color: white;
  border-radius: 16px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
  padding: 30px;
  position: relative;
  z-index: 1;
  animation: fadeInUp 0.8s ease-out;
}

@keyframes fadeInUp {
  from {
    transform: translateY(30px);
    opacity: 0;
  }
  to {
    transform: translateY(0);
    opacity: 1;
  }
}

/* Logo区域样式 */
.logo-area {
  display: flex;
  flex-direction: column;
  align-items: center;
  margin-bottom: 30px;
}

.logo-image {
  width: 80px;
  height: 80px;
  border-radius: 16px;
  object-fit: cover;
  margin-bottom: 16px;
  box-shadow: 0 4px 10px rgba(0, 0, 0, 0.08);
}

.app-title {
  font-size: 24px;
  font-weight: 600;
  color: #333;
  margin-bottom: 6px;
  letter-spacing: 1px;
}

.app-slogan {
  font-size: 14px;
  color: #888;
  text-align: center;
}

/* 表单区域样式 */
.form-area {
  margin-bottom: 20px;
}

.form-title {
  font-size: 20px;
  font-weight: 600;
  color: #333;
  margin-bottom: 25px;
  text-align: center;
}

.input-group {
  margin-bottom: 25px;
  position: relative;
}

.custom-input {
  background-color: #f8f9fb;
  border-radius: 8px;
  border: 1px solid transparent;
  transition: all 0.3s ease;
}

.custom-input:hover, .custom-input:focus {
  border-color: #42a68a;
  background-color: white;
}

.input-bar {
  position: absolute;
  bottom: 0;
  left: 50%;
  width: 0;
  height: 2px;
  background: linear-gradient(to right, #4267d4, #42a68a);
  transition: all 0.3s ease;
  transform: translateX(-50%);
}

.custom-input:focus ~ .input-bar {
  width: 100%;
}

.input-hint {
  font-size: 12px;
  margin-top: 6px;
  display: flex;
  align-items: center;
  gap: 4px;
}

.success {
  color: #52c41a;
}

.password-hint {
  color: #ff4d4f;
  font-size: 12px;
  margin-top: 6px;
  position: absolute;
  bottom: -20px;
}

/* 协议区域样式 */
.agreement-check {
  margin-bottom: 20px;
}

.agreement-text {
  font-size: 12px;
  color: #888;
}

.agreement-link {
  color: #4267d4;
  cursor: pointer;
  transition: color 0.3s ease;
}

.agreement-link:hover {
  color: #42a68a;
  text-decoration: underline;
}

/* 注册按钮样式 */
.register-btn {
  height: 50px;
  margin-bottom: 15px;
  background: linear-gradient(45deg, #4267d4, #42a68a);
  color: white;
  font-size: 16px;
  font-weight: 500;
  border: none;
  box-shadow: 0 4px 15px rgba(66, 166, 138, 0.3);
  transition: all 0.3s ease;
}

.register-btn:hover, .register-btn:active {
  transform: translateY(-2px);
  box-shadow: 0 6px 20px rgba(66, 166, 138, 0.4);
}

/* 登录链接 */
.login-link {
  text-align: center;
  font-size: 14px;
  color: #666;
  cursor: pointer;
  transition: color 0.3s ease;
}

.login-link .highlight {
  color: #4267d4;
  font-weight: 500;
}

.login-link:hover .highlight {
  color: #42a68a;
  text-decoration: underline;
}

/* 注册优势 */
.register-benefits {
  display: flex;
  justify-content: space-around;
  margin-top: 30px;
  padding-top: 20px;
  border-top: 1px dashed #eee;
}

.benefit-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
}

.benefit-icon {
  font-size: 24px;
  color: #4267d4;
}

.benefit-text {
  font-size: 12px;
  color: #666;
}

/* 响应式调整 */
@media screen and (max-width: 480px) {
  .register-card {
    padding: 20px;
    max-width: 340px;
  }
  
  .logo-image {
    width: 70px;
    height: 70px;
  }
  
  .app-title {
    font-size: 22px;
  }
  
  .form-title {
    font-size: 18px;
  }
}
</style>